<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #app {
            width: 552px;
            margin: 100px auto;

        }

        .box {
            height: 35px;
            border: 1px solid gainsboro;
            box-sizing: border-box;
        }

        .box input,
        button {
            border: none;
            box-sizing: border-box;

        }

        .box input {
            width: 400px;
            outline-style: none;
        }

        .box button {
            width: 150px;
            height: 100%;
            background-color: #4e6ef2;
            color: #fff;
        }

        .list {
            border: 1px solid gainsboro;
        }

        .list li {
            list-style: none;
        }

        .active {
            background-color: pink;
            color: #4e6ef2;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="box">
            <input type="text" v-model="msg" @input="server()" @keydown.down.prevent="down" @keydown.up.prevent="up"><button>百度一下</button>
        </div>
        <ul class="list">
            <li v-for="(item,index) in arr" :class="{active:listIndex==index}">
                {{item}}  
            </li>
        </ul>
    </div>
</body>
<script src="js/vue.js"></script>
<script src="js/vue-resource.js"></script>
<script>
    let vm = new Vue({
        el: "#app",
        data: {
            msg: '',
            arr: [],
            listIndex: 0
        },
        methods: {
            server() {
                if (!this.msg == '') {
                    this.$http.jsonp("https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?", {
                        params: {
                            wd: this.msg
                        },
                        jsonp: "cb"
                    })
                        .then(res => {
                            console.log(res.body.s);
                            this.arr = res.body.s
                        }).catch(err => {
                            console.log(err);
                        })
                }
            },
            down() {
                this.listIndex++
                if(this.listIndex>this.arr.length-1){
                    this.listIndex = 0
                }
            },
            up() {
                this.listIndex--
                if(this.listIndex<0){
                    this.listIndex = this.arr.length-1
                }
            }
        }
    })

    

</script>

</html>